package com.linkwechat.common.utils;


import com.linkwechat.common.enums.ResponseEnum;
import com.linkwechat.common.exception.BusinessException;
import com.linkwechat.common.vo.CommonRsp;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;

import java.util.Collection;
import java.util.Map;

/**
 * @author wjp
 * @version 1.0
 */
public class ParamCheckUtil {

    /**
     * 检查对象是否为null
     * @param o
     * @param msg
     */
    public static void objectNull(Object o,String msg){
        if(o == null){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }


    /**
     * 检查对象是否不为null
     * @param o
     * @param msg
     */
    public static void objectNotNull(Object o,String msg){
        if(o != null){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查字符串是否为空
     * @param str
     * @param msg
     */
    public static void stringEmpty(String str,String msg){
        if(StringUtil.isEmpty(str)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检如果全都为空  报错
     * @param str
     * @param msg
     */
    public static void stringAllEmpty(String msg,String... str){
        boolean isAllEmpty = true;
        for(int i = 0; i < str.length;i++){
            if(StringUtil.isNotEmpty(str[i])){
                isAllEmpty = false;
                break;
            }
        }
        if(isAllEmpty){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检如果全都为空  报错
     * @param str
     * @param msg
     */
    public static void stringNotAllEmpty(String msg,String... str){
        boolean isNotAllEmpty = false;
        for(int i = 0; i < str.length;i++){
            if(StringUtil.isNotEmpty(str[i])){
                isNotAllEmpty = true;
                break;
            }
        }
        if(isNotAllEmpty == false){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    public static void numberLessOrEqualZero(Integer num,String msg) {
        if(num <=0){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    public static void numberLessOrEqualZero(Long num,String msg) {
        if(num <=0){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查字2符串是否相等
     * @param str1
     * @param str2
     * @param msg
     */
    public static void notEqual(String str1,String str2,String msg){
        if(str1 != null && str2 != null && !str1.equals(str2)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查数字是否相等
     * @param num1
     * @param num2
     * @param msg
     */
    public static void notEqual(int num1,int num2,String msg){
        if(num1 != num2){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     *将str与compare进行比较，如果与任何一个都不相同 则报错
     * @param str
     * @param compare
     */
    public static void notEqualAny(String str,String msg,String... compare){
        if(StringUtil.isEmpty(str)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
        boolean isEqualAny = false;
        for(String str1 : compare){
            if(str.equals(str1)){
                isEqualAny = true;
                break;
            }
        }
        if(isEqualAny == false){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     *将str与compare进行比较，如果与任何一个相同 则报错
     * @param str
     * @param compare
     */
    public static void equalAny(String str,String msg,String... compare){
        if(StringUtil.isEmpty(str)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
        boolean isEqualAny = false;
        for(String str1 : compare){
            if(str.equals(str1)){
                isEqualAny = true;
                break;
            }
        }
        if(isEqualAny == true){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     *将str与compare进行比较，如果与任何一个都不相同 则报错
     * @param str
     * @param compare
     */
    public static void notEqualAnyInteger(Integer str,String msg,Integer... compare){
        if(str == null){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
        boolean isEqualAny = false;
        for(Integer str1 : compare){
            if(str.equals(str1)){
                isEqualAny = true;
                break;
            }
        }
        if(isEqualAny == false){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     *将num与compare进行比较，如果与任何一个都不相同 则报错
     * @param num
     * @param compare
     */
    public static void notEqualAny(int num,String msg,int... compare){
        boolean isEqualAny = false;
        for(int num1 : compare){
            if(num == num1){
                isEqualAny = true;
                break;
            }
        }
        if(isEqualAny == false){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查：为假，报错
     * @param isTrue
     * @param msg
     */
    public static void notTrue(Boolean isTrue,String msg){
        if(isTrue == null || isTrue == false){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查：为真，报错
     * @param isTrue
     * @param msg
     */
    public static void isTrue(Boolean isTrue,String msg){
        if(isTrue != null && isTrue == true){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查集合是否不为空
     * @param coll
     * @param msg
     * @param <T>
     */
    public static  <T> void collectionNotEmpty(Collection<T> coll,String msg){
        if(!CollectionUtils.isEmpty(coll)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查集合是否为空
     * @param coll
     * @param msg
     * @param <T>
     */
    public static  <T> void collectionEmpty(Collection<T> coll, String msg){
        if(CollectionUtils.isEmpty(coll)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查集合是否为空
     * @param map
     * @param msg
     * @param <T>
     */
    public static  <T> void collectionEmpty(Map<?,?> map, String msg){
        if(CollectionUtils.isEmpty(map)){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 检查集合是否多于1个元素
     * @param coll
     * @param msg
     * @param <T>
     */
    public static  <T> void collectionMoreThanOne(Collection<T> coll, String msg){
        if(coll != null && coll.size() > 1){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }

    /**
     * 如果数组为空 报错
     * @param arr
     * @param msg
     * @param <T>
     */
    public static  <T> void arrayEmpty(T[] arr, String msg){
        if(arr == null || arr.length == 0){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg);
        }
    }


    public static void verifyBindingResult(BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new BusinessException(ResponseEnum.FAIL.getCode(),bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
    }

    public static <T> void notValidate(Validator validator, T obj){
        ParamCheckUtil.verifyBindingResult(ValidatorUtil.validate(validator,obj));
    }

    /**
     * 对feign接口的返回，进行调用成功与否的判断 如果失败，摆错
     * @param commonRsp
     * @return
     */
    public static void feignResCheck(CommonRsp commonRsp, String msg){
        if(commonRsp == null){
            return;
        }
        Integer code = commonRsp.getCode();
        if(code == null || ResponseEnum.SUCCESS.getCode() != code.intValue()){
            throw new BusinessException(ResponseEnum.FAIL.getCode(),msg != null ?  msg : commonRsp.getMsg());
        }
    }

}
